package com.huawang.ecolink.apps.common;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class PathHandleUtil {

    private PathHandleUtil() {

    }

    /***
     * @Description: 获取经过的所有路径
     * @param path String
     * @param separator String
     * @return: java.util.List<java.lang.String>
     * @throws:
     */
    public static List<String> getPassPaths(String path, String separator) {
        List<String> passPaths = new ArrayList();
        if (StringUtils.isNotBlank(path)) {
            passPaths.add(path);
            passPaths.addAll(getPassPaths(getParentPath(path, separator), separator));
        }
        return passPaths;
    }

    /***
     * @Description: 获取父节点路径
     * @param path String
     * @param separator String
     * @return: java.lang.String
     * @throws:
     */
    public static String getParentPath(String path, String separator) {
        if (StringUtils.isNotBlank(path) && StringUtils.isNotBlank(separator)) {
            int i = path.lastIndexOf(separator);
            if (i > 0) {
                return path.substring(0, i);
            }

        }
        return "";
    }


    /***
     * @Description: 获取空间下的所有路径
     * @param path String
     * @param passPaths List<String>
     * @param namespace String
     * @return: void
     * @throws:
     */
    public static void getAllPaths(String path, List<String> passPaths, String namespace) {
        passPaths.add(path);
        if (StringUtils.isNotBlank(path)) {
            int i = path.lastIndexOf(".");
            if (i > 0) {
                String substring = path.substring(0, i);
                getAllPaths(substring, passPaths, namespace);
            }

        }
    }

    /***
     * @Description: 获取路径中模型的名称
     * @param path String
     * @param separator String
     * @return: java.lang.String
     * @throws:
     */
    public static String getPathOfName(String path, String separator) {
        if (StringUtils.isNotBlank(path)) {

            return path.contains(separator) ? path.substring(path.lastIndexOf(separator)).replace(separator, "") : path;
        }
        return null;
    }

    /***
     * @Description: 匹配率
     * @param s1 String
     * @param s2 String
     * @return: double
     * @throws:
     */
    public static double stringSimilarity(String s1, String s2) {
        int len1 = s1.length();
        int len2 = s2.length();
        int common = 0;
        for (int i = 0; i < len1; i++) {
            for (int j = 0; j < len2; j++) {
                if (s1.charAt(i) == s2.charAt(j)) {
                    common++;
                    break;
                }
            }
        }
        return (double) common / (double) Math.max(len1, len2);
    }

}
